package com.hualan.sfzy.service.course.impl;

import com.hualan.sfzy.entity.CourseCollect;
import com.hualan.sfzy.entity.CourseOrder;
import com.hualan.sfzy.mapper.course.CourseCollectMapper;
import com.hualan.sfzy.mapper.course.CourseDetailMapper;
import com.hualan.sfzy.mapper.course.CourseOrderMapper;
import com.hualan.sfzy.service.course.CourseDetailService;
import com.hualan.sfzy.utils.TimeConverter;
import com.hualan.sfzy.vo.CourseDetailVo;
import com.hualan.sfzy.vo.CourseVideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CourseDetailServiceImpl implements CourseDetailService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Autowired
    private CourseCollectMapper courseCollectMapper;

    @Override
    public int add(CourseDetailVo entity) {
        return 0;
    }

    @Override
    public int edit(CourseDetailVo entity) {
        return 0;
    }

    @Override
    public int remove(Integer id) {
        return 0;
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return 0;
    }

    @Override
    public List<CourseDetailVo> find() {
        return null;
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseDetailVo entity) {
        return null;
    }

    @Override
    public CourseDetailVo findById(Integer id) {
        return null;
    }

    @Override
    public CourseDetailVo selectDetailById(Integer id,Integer userId) {
        List<CourseDetailVo> list = courseDetailMapper.selectDetailById(id);
        CourseDetailVo courseDetailVo = list.isEmpty() ? null : list.get(0);
        if(courseDetailVo.getTime()!=null)
            courseDetailVo.setTimeStr(TimeConverter.toMinSecStr(courseDetailVo.getTime()));
        for (CourseVideoVo co: courseDetailVo.getVideo()
        ) {
            if(co.getTime()!=null)
                co.setTimeStr(TimeConverter.toMinSecStr(co.getTime()));
        }
        int num = 0;
        for (CourseCollect cm: courseDetailVo.getCollect()
             ) {
            if(cm.getDel()==0)
                num++;
        }
        courseDetailVo.setCollectCount(num);
        System.out.println("整体数据："+courseDetailVo);


        List<CourseOrder> orders = new ArrayList<>();

        List<CourseOrder> orders1 = courseOrderMapper.selectByIds(userId);
        for (CourseOrder order: orders1
             ) {
            if(order.getCourseId().equals(id))
                orders.add(order);
        }

        courseDetailVo.setOrder(orders);
        System.out.println(orders);


        List<CourseCollect> collects = new ArrayList<>();
        List<CourseCollect> collects1 = courseCollectMapper.selectByIds(userId);
        for (CourseCollect collect: collects1
             ) {
            if(collect.getCourseId().equals(id))
                collects.add(collect);
        }
        courseDetailVo.setCollect(collects);
        return courseDetailVo;
    }
}
